﻿using Intex.Tools;
using INTEX.DataBase.Tools;
using ReflectionStudio.Core.Events;
using System;
using System.Collections;
using System.Data;
using System.Linq;
using Intex.Tools;
using INTEX.DataBase;
using INTEX.DataBase.Tools;
using ReflectionStudio.Core;
using ReflectionStudio.Core.Events;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.OracleClient;
using System.IO;
using WPFExcelReport.Helpers;
using INTEX.DataBase;
namespace WPFExcelReport
{

    public partial class ITXABPLANData : BaseFunction
    {
        string ABPLFD = "X";
        int ABPOS = 1;
        public decimal LDAB05 = 1;
        public decimal LDAB55 = 1;
        public decimal LDAB45 = 1;
        public decimal LDAB50 = 1;
        DataSet SetDispo = new DataSet();
        Queue<DataSet> JobQueue;
        DataSet Set4ITX = new DataSet();
        DataRow dwIntexStd = null;
        bool IsRun = false;
        string PRLO = "", PRLB = "", PRLP = "";
        string time;
        int ABAID = 1;
        public DataSet SetMap;
        public Convert2Intex Convert2Intex;
        public DataSet SetExt = new System.Data.DataSet();
        public DataSet SetJob;
        public SETINTEXDB SetData;
        DataTable tbABAUFM = null;
        INTEXDBC INTEXDBC;
        SETINTEXDB INTEXDB = new INTEX.DataBase.SETINTEXDB();
        string processed = "";
        Dictionary<string, string> lstABPOS = new Dictionary<string, string>();
        public ITXABPLANData()
            : base()
        {
            Helpers.Helper.GetDate(ref date, ref year, ref month, ref day);
        }
        public ITXABPLANData(OracleConnection _OracleDB, IntexReptServBLClient _proxy, string userID = "        ")
            : base(_OracleDB, _proxy, userID)
        {
            INTEXDBC = new INTEXDBC(_OracleDB, _proxy);
            Convert2Intex = new Convert2Intex();
            Helpers.Helper.GetDate(ref date, ref year, ref month, ref day);


        }
        public ITXABPLANData(Dictionary<string, object> DicGlobalPara)
            : base(DicGlobalPara)
        {
            INTEXDBC = new INTEXDBC(OracleDB, proxy);
            Convert2Intex = new Convert2Intex();
            JobQueue = new Queue<DataSet>();
            Helpers.Helper.GetDate(ref date, ref year, ref month, ref day);
        }

        public DataSet LoadEnv()
        {
            DataSet SResult = new DataSet();
            DataSet SReturn = new DataSet();
            Helper.GetDate(ref date, ref year, ref month, ref day);
            SetMap = new DataSet();
            DataSet STemp = new System.Data.DataSet();
            string filePath = Path.Combine(PathHelper.ApplicationPath, "Report\\Interface\\JLTAV95MAPINTEX.XML");
            STemp.ReadXml(filePath);
            SetMap.Merge(STemp);
            filePath = Path.Combine(PathHelper.ApplicationPath, @"Report\Interface\PRODSETUP.XML");
            if (!System.IO.File.Exists(filePath))
            {
                return SReturn;
            }
            SResult.ReadXml(filePath);
            SReturn.Merge(SResult);
            DataRow dwData = INTEXDB.APFOLGE.NewRow();
            dwData["APFMANDANT"] = "00";
            SResult = INTEXDBC.QueryAPFOLGE(dwData, 3);
            SReturn.Merge(SResult);
            return SReturn;
        }

        public DataSet LoadData(DataRow dwInput, string WTYPE = "K")
        {
            DataSet SReturn = new DataSet();
            DataSet SResult = new DataSet();
            DataSet STemp = new System.Data.DataSet();
            OracleCommand or1;
            string KRS = "";
            string LFD = "";
            string POS = "";
            DataRow dwData;
            try
            {
                KRS = dwInput["VKPAUFKRS"].ToString();
                LFD = dwInput["VKPAUFLFD"].ToString();
                POS = dwInput["VKPAUFPOS"].ToString();
                switch (WTYPE)
                {
                    case "K":
                        #region=============
                        dwData = INTEXDB.VKKOPF.NewRow();
                        dwData["VKKMANDANT"] = "00";
                        dwData["VKKAUFKRS"] = KRS;
                        dwData["VKKAUFLFD"] = LFD;
                        SResult = INTEXDBC.QueryVKKOPF(dwData, 1);
                        SReturn.Merge(SResult);
                        dwData = INTEXDB.VKPOS.NewRow();
                        dwData["VKPMANDANT"] = "00";
                        dwData["VKPAUFKRS"] = KRS;
                        dwData["VKPAUFLFD"] = LFD;
                        SResult = INTEXDBC.QueryVKPOS(dwData, 2);
                        SReturn.Merge(SResult);
                        dwData = INTEXDB.WDECK.NewRow();
                        dwData["WDMANDANT"] = "00";
                        dwData["WDAUFKRS"] = KRS;
                        dwData["WDAUFLFD"] = LFD;
                        SResult = INTEXDBC.QueryAPZUORD(dwData, 2);
                        SReturn.Merge(SResult);
                        SResult = INTEXDBC.QueryAPZUORD(dwData, 3);
                        SReturn.Merge(SResult);
                        SResult = INTEXDBC.QueryAPZUORD(dwData, 4);
                        SReturn.Merge(SResult);
                        SResult = INTEXDBC.QueryAPZUORD(dwData, 5);
                        SReturn.Merge(SResult);
                        dwData = INTEXDB.WDECK.NewRow();
                        dwData["WDMANDANT"] = "00";
                        dwData["WDAUFKRS"] = KRS;
                        dwData["WDAUFLFD"] = LFD;
                        STemp = new System.Data.DataSet();
                        if (SReturn.Tables.Contains("APZUORD"))
                        {
                            string processed = "";
                            foreach (DataRow rx in SReturn.Tables["APZUORD"].Rows)
                            {
                                string APZANRKRS = rx["APZANRKRS"].ToString();
                                string APZALFDNR = rx["APZALFDNR"].ToString();
                                if (processed.Contains(APZANRKRS + APZALFDNR + ";"))
                                    continue;
                                processed += APZANRKRS + APZALFDNR + ";";
                                dwData = INTEXDB.APFOLGE.NewRow();
                                dwData["APFMANDANT"] = rx["APZMANDANT"];
                                dwData["APFANRKRS"] = APZANRKRS;
                                dwData["APFALFDNR"] = APZALFDNR;
                                SResult = INTEXDBC.QueryAPFOLGE(dwData, 4);
                                STemp.Merge(SResult);
                                dwData = INTEXDB.APARTK.NewRow();
                                dwData["APAKMANDANT"] = rx["APZMANDANT"];
                                dwData["APAKANRKRS"] = APZANRKRS;
                                dwData["APAKALFDNR"] = APZALFDNR;
                                SResult = INTEXDBC.QueryAPARTK(dwData, 1);
                                STemp.Merge(SResult);
                            }
                            SReturn.Merge(STemp);
                        }
                        dwData = INTEXDB.WDECK.NewRow();
                        dwData["WDMANDANT"] = "00";
                        dwData["WDAUFKRS"] = KRS;
                        dwData["WDAUFLFD"] = LFD;
                        SResult = INTEXDBC.QueryWDECK(dwData, 4);
                        SReturn.Merge(SResult);
                        dwData = INTEXDB.WDECK.NewRow();
                        dwData["WDMANDANT"] = "00";
                        dwData["WDAUFKRS"] = KRS;
                        dwData["WDAUFLFD"] = LFD;
                        SResult = INTEXDBC.QueryWASTAMM(dwData, 5);
                        SReturn.Merge(SResult);
                        dwData = INTEXDB.WDECK.NewRow();
                        dwData["WDMANDANT"] = "00";
                        dwData["WDAUFKRS"] = KRS;
                        dwData["WDAUFLFD"] = LFD;
                        SResult = INTEXDBC.QueryWASTKE(dwData, 2);
                        SReturn.Merge(SResult);
                        dwData = INTEXDB.WDECK.NewRow();
                        dwData["WDMANDANT"] = "00";
                        dwData["WDAUFKRS"] = KRS;
                        dwData["WDAUFLFD"] = LFD;
                        dwData["WDSA"] = "C";
                        SResult = INTEXDBC.QueryWAFARBEN(dwData, 6);
                        SReturn.Merge(SResult);
                        dwData = INTEXDB.WDECK.NewRow();
                        dwData["WDMANDANT"] = "00";
                        dwData["WDAUFKRS"] = KRS;
                        dwData["WDAUFLFD"] = LFD;
                        dwData["WDSA"] = "B";
                        SResult = INTEXDBC.QueryWAFARBEN(dwData, 7);
                        SReturn.Merge(SResult);

                        dwData = INTEXDB.WDECK.NewRow();
                        dwData["WDMANDANT"] = "00";
                        dwData["WDAUFKRS"] = KRS;
                        dwData["WDAUFLFD"] = LFD;
                        SResult = INTEXDBC.QueryWASTGA(dwData, 4);
                        SReturn.Merge(SResult);
                        dwData = INTEXDB.WDECK.NewRow();
                        dwData["WDMANDANT"] = "00";
                        dwData["WDAUFKRS"] = KRS;
                        dwData["WDAUFLFD"] = LFD;
                        SResult = INTEXDBC.QueryBSSATZ(dwData, 2);
                        SReturn.Merge(SResult);
                        dwData = INTEXDB.WDECK.NewRow();
                        dwData["WDMANDANT"] = "00";
                        dwData["WDAUFKRS"] = KRS;
                        dwData["WDAUFLFD"] = LFD;
                        SResult = INTEXDBC.QueryBSGROE(dwData, 2);
                        SReturn.Merge(SResult);
                        dwData = INTEXDB.WDECK.NewRow();
                        dwData["WDMANDANT"] = "00";
                        dwData["WDAUFKRS"] = KRS;
                        dwData["WDAUFLFD"] = LFD;
                        SResult = INTEXDBC.QueryBHSATZ(dwData, 2);
                        SReturn.Merge(SResult);
                        dwData = INTEXDB.WDECK.NewRow();
                        dwData["WDMANDANT"] = "00";
                        dwData["WDAUFKRS"] = KRS;
                        dwData["WDAUFLFD"] = LFD;
                        SResult = INTEXDBC.QueryBASTGA(dwData, 2);
                        SReturn.Merge(SResult);
                        STemp = new System.Data.DataSet();
                        string yarns = "";
                        string Yart = "";
                        string Yvar = "";
                        foreach (DataRow row in SReturn.Tables["WASTGA"].Rows)
                        {
                            dwData = INTEXDB.GSDAT.NewRow();
                            Yart = row["WAGGART"].ToString();
                            Yvar = row["WAGGVAR"].ToString();
                            if (yarns.Contains(Yart + Yvar + ";"))
                            {
                                continue;
                            }
                            yarns += Yart + Yvar + ";";
                            dwData["GSMANDANT"] = row["WAGMANDANT"];
                            dwData["GSART"] = Yart;
                            dwData["GSVAR"] = Yvar;
                            SResult = INTEXDBC.QueryGSDAT(dwData);
                            STemp.Merge(SResult);
                        }
                        SReturn.Merge(STemp);
                        STemp = new System.Data.DataSet();
                        foreach (DataRow row in SReturn.Tables["BASTGA"].Rows)
                        {
                            Yart = row["BAGGART"].ToString();
                            Yvar = row["BAGGVAR"].ToString();
                            if (processed.Contains(Yart + Yvar + ";"))
                            {
                                continue;
                            }
                            processed += Yart + Yvar + ";";
                            dwData = INTEXDB.GSDAT.NewRow();
                            dwData["GSMANDANT"] = row["BAGMANDANT"];
                            dwData["GSART"] = Yart;
                            dwData["GSVAR"] = Yvar;
                            SResult = INTEXDBC.QueryGSDAT(dwData);
                            STemp.Merge(SResult);
                            dwData = INTEXDB.GASTGA.NewRow();
                            dwData["GAGMANDANT"] = row["BAGMANDANT"];
                            dwData["GAGART"] = Yart;
                            SResult = INTEXDBC.QueryGASTGA(dwData, 2);
                            STemp.Merge(SResult);
                        }
                        SReturn.Merge(STemp);
                        foreach (DataRow row in SReturn.Tables["GSDAT"].Rows)
                        {
                            dwData = INTEXDB.GASTGA.NewRow();
                            dwData["GAGMANDANT"] = row["GSMANDANT"];
                            dwData["GAGART"] = row["GSART"];
                            SResult = INTEXDBC.QueryGASTGA(dwData, 2);
                            STemp.Merge(SResult);
                        }
                        SReturn.Merge(STemp);
                        #endregion
                        break;
                    case "V":
                        #region=============
                        dwData = INTEXDB.ABKOPF.NewRow();
                        dwData["ABKMANDANT"] = "00";
                        dwData["ABKAUFKRS"] = KRS;
                        dwData["ABKAUFLFD"] = LFD;
                        SResult = INTEXDBC.QueryABKOPF(dwData, 1);
                        SReturn.Merge(SResult);
                        dwData = INTEXDB.ABPOS.NewRow();
                        dwData["ABPMANDANT"] = "00";
                        dwData["ABPAUFKRS"] = KRS;
                        dwData["ABPAUFLFD"] = LFD;
                        dwData["ABPAUFPOS"] = POS;
                        SResult = INTEXDBC.QueryABPOS(dwData, 1);
                        SReturn.Merge(SResult);
                        dwData = INTEXDB.WDECK.NewRow();
                        dwData["WDMANDANT"] = "00";
                        dwData["WDAUFKRS"] = KRS;
                        dwData["WDAUFLFD"] = LFD;
                        dwData["WDAUFPOS"] = POS;
                        SResult = INTEXDBC.QueryWDECK(dwData, 18);
                        SReturn.Merge(SResult);
                        dwData = INTEXDB.ABAUFM.NewRow();
                        dwData["ABAMANDANT"] = "00";
                        dwData["ABAAUFKRS"] = KRS;
                        dwData["ABAAUFLFD"] = LFD;
                        dwData["ABAAUFPOS"] = POS;
                        SResult = INTEXDBC.QueryABAUFM(dwData, 5);
                        SReturn.Merge(SResult);
                        dwData = INTEXDB.ABPR.NewRow();
                        dwData["ARMANDANT"] = "00";
                        dwData["ARAUFKRS"] = KRS;
                        dwData["ARAUFLFD"] = LFD;
                        dwData["ARAUFPOS"] = POS;
                        SResult = INTEXDBC.QueryABPR(dwData, 1);
                        SReturn.Merge(SResult);
                        dwData = INTEXDB.ABFOLGE.NewRow();
                        dwData["ABFMANDANT"] = "00";
                        dwData["ABFAUFKRS"] = KRS;
                        dwData["ABFAUFLFD"] = LFD;
                        dwData["ABFAUFPOS"] = POS;
                        SResult = INTEXDBC.QueryABFOLGE(dwData, 1);
                        SReturn.Merge(SResult);
                        dwData = INTEXDB.ABARTE.NewRow();
                        dwData["ABEMANDANT"] = "00";
                        dwData["ABEAUFKRS"] = KRS;
                        dwData["ABEAUFLFD"] = LFD;
                        dwData["ABEAUFPOS"] = POS;
                        SResult = INTEXDBC.QueryABARTE(dwData, 2);

                        SReturn.Merge(SResult);
                        dwData = INTEXDB.BAAUF.NewRow();
                        dwData["BAMANDANT"] = "00";
                        dwData["BAAUFKRS"] = KRS;
                        dwData["BAAUFLFD"] = LFD;
                        dwData["BAAUFPOS"] = POS;
                        SResult = INTEXDBC.QueryBAAUF(dwData, 2);
                        SReturn.Merge(SResult);
                        dwData = INTEXDB.BABST.NewRow();
                        dwData["BMMANDANT"] = "00";
                        dwData["BMAUFKRS"] = KRS;
                        dwData["BMAUFLFD"] = LFD;
                        dwData["BMAUFPOS"] = POS;
                        SResult = INTEXDBC.QueryBABST(dwData, 2);
                        SReturn.Merge(SResult);
                        dwData = INTEXDB.GAAUF.NewRow();
                        dwData["GAMANDANT"] = "00";
                        dwData["GAAUFKRS"] = KRS;
                        dwData["GAAUFLFD"] = LFD;
                        dwData["GAAUFPOS"] = POS;
                        SResult = INTEXDBC.QueryGAAUF(dwData, 2);
                        SReturn.Merge(SResult);
                        #endregion
                        break;
                }

            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            return SReturn;
        }

        public void DeleteABON(string krs, string lfd, string pos, string company = "DST")
        {
            DataSet setReturn = new DataSet();
            DataSet setResult = new DataSet();
            OracleCommand or1;
            try
            {
                DataRow dwData = INTEXDB.ABAUFM.NewRow();
                dwData["ABAMANDANT"] = "00";
                dwData["ABAAUFKRS"] = krs;
                dwData["ABAAUFLFD"] = lfd;
                dwData["ABAAUFPOS"] = pos;
                setResult = INTEXDBC.QueryABAUFM(dwData, 1);
                if (setResult.Tables[0].Rows.Count == 0)
                    return;
                foreach (DataRow dwABAUFM in setResult.Tables[0].Rows)
                {
                    or1 = OracleDB.CreateCommand();
                    string akrs = dwABAUFM["ABAAUFKRS"].ToString();
                    string alfd = dwABAUFM["ABAAUFLFD"].ToString();
                    string apos = dwABAUFM["ABAAUFPOS"].ToString();
                    or1.CommandText = "DELETE_ORDER_NETWORK";
                    or1.CommandType = CommandType.StoredProcedure;
                    or1.Parameters.Add("AUFKRS", OracleType.NVarChar).Value = akrs;
                    or1.Parameters.Add("AUFLFD", OracleType.NVarChar).Value = alfd;
                    or1.Parameters.Add("AUFPOS", OracleType.NVarChar).Value = apos;
                    Connect2Proxy(or1);
                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        public void DeleteAV95(string krs, string lfd, string pos)
        {
            lstABPOS = new Dictionary<string, string>();
            string key = krs + "_" + lfd + "_" + pos;
            lstABPOS.Add(key, "NO");
            while (lstABPOS.ContainsValue("NO"))
            {
                Queue<string> qabp = new Queue<string>();
                foreach (KeyValuePair<string, string> kp in lstABPOS)
                {
                    if (kp.Value == "YES")
                        continue;
                    qabp.Enqueue(kp.Key);
                }
                while (qabp.Count > 0)
                {
                    GetOrderNetWork(qabp.Dequeue());
                }
            }
            lstABPOS.Remove(key);
            foreach (KeyValuePair<string, string> kp in lstABPOS)
            {
                key = kp.Key;
                 krs = key.Substring(0, 2);
                 lfd = key.Substring(3, 6);
                 pos = key.Substring(10, 3);
                 Delete("00", krs, lfd, pos);
            }

        }
        private void GetOrderNetWork(string key)
        {
            string krs = key.Substring(0, 2);
            string lfd = key.Substring(3, 6);
            string pos = key.Substring(10, 3);
            //T6-001230-001
            lstABPOS[key] = "YES";
            DataRow dwData = INTEXDB.ABAUFM.NewRow();
            dwData["ABAMANDANT"] = "00";
            dwData["ABAVKAUFKRS"] = krs;
            dwData["ABAVKAUFLFD"] = lfd;
            dwData["ABAVKAUFPOS"] = pos;
            DataSet SResult = INTEXDBC.QueryABAUFM(dwData, 6);
            foreach (DataRow rx in SResult.Tables[0].Rows)
            {
                key = rx["ABAAUFKRS"].ToString() + "_" + rx["ABAAUFLFD"].ToString() + "_" + rx["ABAAUFPOS"].ToString();
                if (!lstABPOS.ContainsKey(key))
                {
                    lstABPOS.Add(key, "NO");
                }
            }
        }
        public void Delete(string client, string krs, string lfd, string pos = "000")
        {
            try
            {
                if (INTEXDBC == null)
                {
                    INTEXDBC = new INTEXDBC(OracleDB, proxy);
                }
                DataRow dwData = INTEXDB.ABPOS.NewRow();
                dwData["ABPMANDANT"] = client;
                dwData["ABPAUFKRS"] = krs;
                dwData["ABPAUFLFD"] = lfd;
                DataSet setResult = INTEXDBC.QueryABPOS(dwData, 2);
                if (!setResult.Tables.Contains("ABPOS"))
                    return;
                int count = setResult.Tables["ABPOS"].Rows.Count;
                int cdel = 0;

                foreach (DataRow row in setResult.Tables["ABPOS"].Rows)
                {
                    string posh = row["ABPAUFPOS"].ToString();
                    if (pos != "000")
                    {
                        if (posh != pos)
                            continue;
                    }
                    INTEXDBC.DeleteABPOS(client, krs, lfd, posh);
                    INTEXDBC.DeleteABPR(client, krs, lfd, posh);
                    INTEXDBC.DeleteABAUFM(client, krs, lfd, posh);
                    INTEXDBC.DeleteABFOLGE(client, krs, lfd, posh);
                    INTEXDBC.DeleteWDECK(client, krs, lfd, posh);
                    INTEXDBC.DeleteGAAUF(client, krs, lfd, posh);
                    INTEXDBC.DeleteBAAUF(client, krs, lfd, posh);
                    INTEXDBC.DeleteBABST(client, krs, lfd, posh);
                    INTEXDBC.DeleteABARTE(client, krs, lfd, posh, 1);
                    cdel++;

                }
                if (cdel == count)
                {
                    INTEXDBC.DeleteABKOPF(client, krs, lfd);
                }

            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
    }
}